package com.example.lastwork2.fragments;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;

import com.example.lastwork2.MainActivity;
import com.example.lastwork2.R;
import com.example.lastwork2.adapter.OrderAdapter;
import com.example.lastwork2.cart.CartActivity;
import com.example.lastwork2.fragments.detail.OrderDetailActivity;
import com.example.lastwork2.model.Order;
import com.example.lastwork2.model.Shop;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

//显示订单列表
public class OrderFragment extends Fragment {

    private ListView orderListKongJian;
    private Button btnCart;

    //适配器对象
    private OrderAdapter orderAdapter;

    //所有订单集合
    private List<Order> orderList;


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        //绑定布局文件
        View orderPage = inflater.inflate(R.layout.order_fragment,null);


        //获取控件对象
        orderListKongJian = orderPage.findViewById(R.id.order_list);
        btnCart = orderPage.findViewById(R.id.cart);

        //在数据库中获取所有订单数据
        getAllOrder();

        //点击订单列表跳转订单详情
        orderListKongJian.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent();
                intent.setClass(
                        getActivity(),
                        OrderDetailActivity.class
                );
                //跳转时获取订单ID
                int num = orderList.size();
                String orderId = String.valueOf(orderList.get(num-position-1).getOrderId());
                intent.putExtra("id",orderId);
                startActivity(intent);


            }
        });

        //跳转到购物车页面
        btnCart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.setClass(
                        getActivity(),
                        CartActivity.class
                );
                startActivity(intent);

            }
        });



        return orderPage;
    }

    @Override
    public void onResume() {
        super.onResume();
        getAllOrder();

    }


    //在数据库中获取所有订单数据
    private void getAllOrder() {
        orderList = new ArrayList<>();
        //向服务器发送请求，获取订单数据
        sendToServer();


    }

    private void sendToServer() {
        String url1 = "http://10.0.2.2:8080/bigwork/order";
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {

                    URL url = new URL(url1+"?userId="+ MainActivity.userId);
                    URLConnection urlConnection = url.openConnection();
                    //接收服务器发来的数据
                    InputStream inputStream = urlConnection.getInputStream();
                    //创建字符流读取数据
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"utf-8"));
                    String str = bufferedReader.readLine();
                    //服务器发送json数据成功
                    System.out.println(str);
                    //关闭流
                    bufferedReader.close();
                    inputStream.close();
                    //解析数据
                    Gson gson = new Gson();
                    //解析服务器发来的订单集合
                    orderList = gson.fromJson(str,new TypeToken<List<Order>>(){}.getType());

//                    System.out.println("解析后的订单数据"+orderList.get(0).getTime());



                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                //在子线程里面绑定适配器
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        //实例化适配器对象
                        orderAdapter = new OrderAdapter(
                               getActivity(),
                                R.layout.order_fragment_item,
                                orderList
                        );
                        //绑定适配器
                        orderListKongJian.setAdapter(orderAdapter);
                    }
                });


            }
        }).start();

    }


}

















